package run.siyuan.util;

import net.rubyeye.xmemcached.MemcachedClient;
import net.rubyeye.xmemcached.exception.MemcachedException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeoutException;

/**
 * @className: MemcachedUtil
 * @Description: TODO
 * @author: siyuan
 * @date: 2022/10/31 16:55
 */
public class MemcachedUtil {

    public static MemcachedClient memcachedClient;

    public MemcachedUtil() {
    }

    public static void setMemcachedClient(MemcachedClient memcachedClient) {
        if (MemcachedUtil.memcachedClient == null){
            MemcachedUtil.memcachedClient = memcachedClient;
        }
    }

    public static void set60(String key, String value) {
        setExp(key, 60, value);
    }

    public static void setExp(String key, Integer i, String value) {
        try {
            memcachedClient.set(key, i, value);
        } catch (TimeoutException e) {
            throw new RuntimeException(e);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } catch (MemcachedException e) {
            throw new RuntimeException(e);
        }
    }

    public static Object get(String key) {
        try {
            return memcachedClient.get(key);
        } catch (TimeoutException e) {
            throw new RuntimeException(e);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } catch (MemcachedException e) {
            throw new RuntimeException(e);
        }
    }


}
